package neu.edu.cn.nuemall.controller;

import com.github.pagehelper.PageInfo;
import neu.edu.cn.nuemall.dto.BooksDTO;

import neu.edu.cn.nuemall.dto.BooksSkuDTO;
import neu.edu.cn.nuemall.dto.BooksTypeGroupDTO;

import neu.edu.cn.nuemall.entity.Comments;
import neu.edu.cn.nuemall.entity.Publish;

import neu.edu.cn.nuemall.entity.ShippingAddress;
import neu.edu.cn.nuemall.entity.Userinfo;
import neu.edu.cn.nuemall.service.BooksService;
import neu.edu.cn.nuemall.service.BooksTypeGroupService;
import neu.edu.cn.nuemall.service.CommentsService;
import neu.edu.cn.nuemall.service.PublishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
public class MallController {

    @Autowired
    private PublishService publishService;
    @Autowired
    private BooksService booksService;
    @Autowired
    private BooksTypeGroupService booksTypeGroupService;
    @Autowired
    private CommentsService commentsService;

    @RequestMapping("/")
    public String mallIndex(@RequestParam(value = "page",defaultValue = "1") Integer page,
                            @RequestParam(value = "count",defaultValue = "4") Integer count,
                            Model model){
        //查询最新小店信息
        PageInfo<Publish> publishList= publishService.listLatesPublishs(page,count);
        model.addAttribute("publishList",publishList);

        //查询最新商品信息
        List<BooksDTO> newBooksList= booksService.listLatestBooks();
        model.addAttribute("booksList",newBooksList);
        return "mall/index";
    }

    @RequestMapping("/admin/index")
    public String adminIndex(@RequestParam(value = "page",defaultValue = "1") Integer page,
                             @RequestParam(value = "count",defaultValue = "4") Integer count,
                             Model model){
        //查询最新小店信息
        PageInfo<Publish> publishList= publishService.listLatesPublishs(page,count);
        model.addAttribute("publishList",publishList);

        //查询最新商品信息
        List<BooksDTO> newBooksList= booksService.listLatestBooks();
        model.addAttribute("booksList",newBooksList);

        return "admin/index";
    }
    @RequestMapping("/admin/book")
    public String adminBook(@RequestParam(value = "page",defaultValue = "1") Integer page,
                            @RequestParam(value = "count",defaultValue = "4") Integer count,
                            Model model){
        //查询最新小店信息
        PageInfo<Publish> publishList= publishService.listLatesPublishs(page,count);
        model.addAttribute("publishList",publishList);

        //查询最新商品信息
        List<BooksDTO> newBooksList= booksService.listLatestBooks();
        model.addAttribute("booksList",newBooksList);

        return "admin/book";
    }
    @RequestMapping("/admin/user")
    public String adminUser(@RequestParam(value = "page",defaultValue = "1") Integer page,
                            @RequestParam(value = "count",defaultValue = "4") Integer count,
                            Model model){
        //查询最新小店信息
        PageInfo<Publish> publishList= publishService.listLatesPublishs(page,count);
        model.addAttribute("publishList",publishList);

        //查询最新商品信息
        List<BooksDTO> newBooksList= booksService.listLatestBooks();
        model.addAttribute("booksList",newBooksList);

        return "admin/user";
    }
    @RequestMapping("/admin/com")
    public String adminCom(@RequestParam(value = "page",defaultValue = "1") Integer page,
                           @RequestParam(value = "count",defaultValue = "4") Integer count,
                           Model model){
        //查询最新小店信息
        PageInfo<Publish> publishList= publishService.listLatesPublishs(page,count);
        model.addAttribute("publishList",publishList);

        //查询最新商品信息
        List<BooksDTO> newBooksList= booksService.listLatestBooks();
        model.addAttribute("booksList",newBooksList);

        return "admin/com";
    }
    @RequestMapping("/admin/buy")
    public String adminBuy(@RequestParam(value = "page",defaultValue = "1") Integer page,
                           @RequestParam(value = "count",defaultValue = "4") Integer count,
                           Model model){
        //查询最新小店信息
        PageInfo<Publish> publishList= publishService.listLatesPublishs(page,count);
        model.addAttribute("publishList",publishList);

        //查询最新商品信息
        List<BooksDTO> newBooksList= booksService.listLatestBooks();
        model.addAttribute("booksList",newBooksList);

        return "admin/buy";
    }

    @RequestMapping("/search")
    public String searchBooks(String keyword,String keyword2,Model model){
        List<BooksDTO> booksDTOList = booksService.searchBooks(keyword,keyword2);
        model.addAttribute("booksList", booksDTOList);
        model.addAttribute("keyword",keyword);
        model.addAttribute("keyword2",keyword2);
        return "/mall/search";
    }


    @RequestMapping("/publish-index")
    public String showPublishInfo(@RequestParam(value = "page",defaultValue = "1") Integer page,
                               String id,String type,Model model){

        //查询店铺信息
        Publish publish= publishService.getPublishById(id);
        model.addAttribute("publish",publish);

        //查询店铺分类信息
        List<BooksTypeGroupDTO> booksTypeGroupDTOList = booksTypeGroupService.listAllBooksTypeGroup(id);
        model.addAttribute("typeGroupList", booksTypeGroupDTOList);

        //查询商品信息
        PageInfo<BooksDTO> booksList = booksService.listPublishBooks(page, id,type);
        model.addAttribute("booksPageInfo", booksList);

        model.addAttribute("selectedTypeId", StringUtils.hasText(type)?type:"");

        return "/mall/publish-index";
    }

    @RequestMapping("/books-detail")
    public String mallBooksDetail(@RequestParam("id") String id,Model model,String sku){

        BooksDTO booksDTO = booksService.getBooksById(id);
        model.addAttribute("books", booksDTO);
        List<String> imageList = new ArrayList<>();
        booksDTO.getBooksSkuList().forEach(booksSkuDTO -> {
            booksSkuDTO.getBooksSkuImgList().forEach(booksSkuImgDTO -> {
                imageList.add(booksSkuImgDTO.getImage());
            });
        });
        model.addAttribute("images", imageList);

        BooksSkuDTO booksSkuDTO =
                StringUtils.hasText(sku) ? booksService.getBookSkuById(sku) : booksDTO.getBooksSkuList().get(0);
        model.addAttribute("selectedSku", booksSkuDTO);

        return "/mall/books-detail";
    }
    @RequestMapping("/my-comments")//评价
    public String myComments(Model model,
                             @RequestParam(value = "page",defaultValue = "1") Integer page,
                             @RequestParam(value = "count",defaultValue = "6") Integer count,
                             HttpSession session){

        Userinfo user=(Userinfo) session.getAttribute("USER_INFO");
        if (user!=null){
            PageInfo<Comments> commentsPageInfo=commentsService.getCommentsList(page,count,user.getId());
            model.addAttribute("commentsList",commentsPageInfo);
        }
        return "/mall/my-comments";
    }
    @PostMapping("/addCom")
    public String addCom(Comments comments, HttpSession session){
        Userinfo user=(Userinfo) session.getAttribute("USER_INFO");
        if (user!=null){
            comments.setName(user.getId());
            String id=comments.getId();
            if (StringUtils.isEmpty(id)){
                commentsService.insertCom(comments);
            }
        }
        return "redirect:/my-comments";
    }
    @PostMapping("/deleteCom")
    public String deleteCom(Comments comments, HttpSession session){
        Userinfo user=(Userinfo) session.getAttribute("USER_INFO");
        if (user!=null) {
            comments.setName(user.getId());
            String id = comments.getId();
            commentsService.delectCom(comments);
        }
        return "redirect:/my-comments";
    }

}
